package org.zhadoop.stream.cql.semanticanalyzer.parser.visitor;

import java.util.Locale;

import org.antlr.v4.runtime.misc.NotNull;
import org.zhadoop.stream.cql.semanticanalyzer.parser.CQLParser;
import org.zhadoop.stream.cql.semanticanalyzer.parser.CQLParser.CaseExpressionContext;
import org.zhadoop.stream.cql.semanticanalyzer.parser.CQLParser.CastExpressionContext;
import org.zhadoop.stream.cql.semanticanalyzer.parser.CQLParser.ColumnNameContext;
import org.zhadoop.stream.cql.semanticanalyzer.parser.CQLParser.ConstNullContext;
import org.zhadoop.stream.cql.semanticanalyzer.parser.CQLParser.ExpressionPreviousContext;
import org.zhadoop.stream.cql.semanticanalyzer.parser.CQLParser.ExpressionWithLaparenContext;
import org.zhadoop.stream.cql.semanticanalyzer.parser.CQLParser.FunctionContext;
import org.zhadoop.stream.cql.semanticanalyzer.parser.context.AtomExpressionContext;
import org.zhadoop.stream.cql.semanticanalyzer.parser.context.ConstantContext;

/**
 * 原子表达式解析的结果
 * 
 * 对一些原子的，不可分割的表达式的解析 包含Null、常量、函数、Cast表达式、case when 表达式，流名称或者别名解析 以及带括号的表达式的解析
 * 表达式优先级规定：括号可以改变表达式的优先级，所以带括号的表达式优先解析
 * 
 * @author zwd
 *
 */
public class AtomExpressionVisitor extends AbsCQLParserBaseVisitor<AtomExpressionContext> {

	/**
	 * 原子类型表达式解析结果
	 */
	private AtomExpressionContext context = null;

	public AtomExpressionVisitor() {
		context = new AtomExpressionContext();
	}

	@Override
	protected AtomExpressionContext defaultResult() {
		return context;
	}

	@Override
	public AtomExpressionContext visitConstNull(@NotNull ConstNullContext ctx) {
		ConstantContext contant = new ConstantContext(null, null);
		context.setConstant(contant);
		return context;
	}

	@Override
	public AtomExpressionContext visitConstant(CQLParser.ConstantContext ctx) {
		ConstantVisitor visitor = new ConstantVisitor();
		context.setConstant(visitor.visit(ctx));
		return context;
	}

	@Override
	public AtomExpressionContext visitFunction(@NotNull FunctionContext ctx) {
		FunctionVisitor visitor = new FunctionVisitor();
		context.setFunction(visitor.visit(ctx));
		return context;
	}

	@Override
	public AtomExpressionContext visitCastExpression(@NotNull CastExpressionContext ctx) {
		CastExpressionVisitor visitor = new CastExpressionVisitor();
		context.setCastExpression(visitor.visit(ctx));
		return context;
	}

	@Override
	public AtomExpressionContext visitCaseExpression(@NotNull CaseExpressionContext ctx) {
		CaseExpressionVisitor visitor = new CaseExpressionVisitor();
		context.setCaseExpression(visitor.visit(ctx));
		return context;
	}

	@Override
	public AtomExpressionContext visitColumnName(@NotNull ColumnNameContext ctx) {
		context.setColumnName(ctx.getText().toLowerCase(Locale.US));
		return context;
	}

	@Override
	public AtomExpressionContext visitExpressionWithLaparen(@NotNull ExpressionWithLaparenContext ctx) {
		ExpressionWithLaparenVisitor visitor = new ExpressionWithLaparenVisitor();
		context.setExpressionWithLaparen(visitor.visit(ctx));
		return context;
	}
	
	@Override
	public AtomExpressionContext visitExpressionPrevious(ExpressionPreviousContext ctx) {
		ExpressionPreviousVisitor visitor = new ExpressionPreviousVisitor();
        context.setPrevious(visitor.visit(ctx));
        return context;
	}

}
